package com.jf.demo;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainmenuList  extends BaseAdapter {
	
	private class buttonViewHolder {
        ImageView appIcon;
        TextView appName;
        Button buttonClose;
    }
    
    private ArrayList<HashMap<String, Object>> mAppList;
    private LayoutInflater mInflater;
    private Context mContext;
    private String[] keyString;
    private int[] valueViewID;
    private buttonViewHolder holder;
    private String buttonText;
    
    public MainmenuList(Context c, ArrayList<HashMap<String, Object>> appList,String bT) {
    	String[] from = new String[] {"ItemImage","ItemWinName", "ItemCloseWin"};
    	int[] to = new int[] {R.id.ItemImage,R.id.ItemWinName,R.id.ItemCloseWin};
        mAppList = appList;
        mContext = c;
        mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        keyString = new String[from.length];
        valueViewID = new int[to.length];
        buttonText = bT;
        System.arraycopy(from, 0, keyString, 0, from.length);
        System.arraycopy(to, 0, valueViewID, 0, to.length);
    }
    
    @Override
    public int getCount() {
        return mAppList.size();
    }

    @Override
    public Object getItem(int position) {
        return mAppList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void removeItem(int position){
    	if(buttonText.indexOf("评")<0){
//    		mAppList.remove(position);
    	}
        this.notifyDataSetChanged();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null) {
            holder = (buttonViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.mainmenulist, null);
            holder = new buttonViewHolder();
            holder.appIcon = (ImageView)convertView.findViewById(valueViewID[0]);
            holder.appName = (TextView)convertView.findViewById(valueViewID[1]);
            holder.buttonClose = (Button)convertView.findViewById(valueViewID[2]);
            convertView.setTag(holder);
        }
        
        HashMap<String, Object> appInfo = mAppList.get(position);
        if (appInfo != null) {
            String aname = (String) appInfo.get(keyString[1]);
            int mid = (Integer)appInfo.get(keyString[0]);
//            int bid = (Integer)appInfo.get(keyString[2]);
            holder.appName.setText(aname);
            holder.appIcon.setImageDrawable(holder.appIcon.getResources().getDrawable(mid));
//            holder.buttonClose.setImageDrawable(holder.buttonClose.getResources().getDrawable(R.drawable.ic_launcher));
            holder.buttonClose.setOnClickListener(new lvButtonListener(position));
            holder.buttonClose.setText(buttonText);
        }        
        return convertView;
    }

    class lvButtonListener implements OnClickListener{
        private int position;

        lvButtonListener(int pos) {
            position = pos;
        }
        
        @Override
        public void onClick(View v) {
            int vid=v.getId();
            if (vid == holder.buttonClose.getId())
                removeItem(position);
        }
    }
	
}
